<!-- 拟采取措施表单 -->
<template>
  <div>
    <a-spin :spinning="spinning">
      <a-form-model
        :model="formBox"
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 12 }"
        :rules="rules"
        ref="ProposedMeasuresCardForm"
        class="requiredFormItem"
      >
        <a-row :gutter="24">
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="拟采取措施"
              prop="measure"
            >
              <BaseTextarea
                :disabled="formType == 'disable'"
                placeholder="请输入"
                v-model="formBox.measure"
                :maxLength="1000"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="自检人">
              <BaseInput
                v-model="formBox.checkUser"
                placeholder="带出所选项目名称，不可修改"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="时间">
              <BaseInput
                v-model="formBox.submitTime"
                placeholder="带出所选项目名称，不可修改"
                disabled
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              class="feedbacks"
              label="附件"
              :wrapper-col="{ lg: { span: 17 }, sm: { span: 17 } }"
            >
              <l-table
                count="5"
                :isClose="formType !== 'disable'"
                business-code="PM36"
                :upload-arrys.sync="formBox.measureFileInfoList"
                :needChineseName="true"
                errorTips="附件名称中必须含有中文"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <div
        class="tool-btns"
        v-if="formType !== 'disable'"
      >
        <a-button
          class="ml20"
          @click="handleClose"
        > 关闭 </a-button>
          <a-button
            class="ml20"
            :loading="loading"
            type="primary"
            @click="addWarningFormHandler"
          > 提交 </a-button>
      </div>
      <!-- 选择审批人 -->
      <selectApprover
        :business-id="businessId"
        :process-config="setProcessConfig"
        :is-close-upper="false"
        @close="closeSelectApprover"
      />
      <!-- 选择审批人 -->
    </a-spin>
  </div>
</template>

<script>
import mixins from '../mixins.js'
import { fillMeasure } from '@/api/project/warning'
export default {
  name: 'ProposedMeasuresCard',
  mixins: [mixins],
  props: ['proposedMeasuresStatus', 'formType', 'form', 'row'],
  data() {
    return {
      loading: false,
      spinning: false,
      formBox: {
        checkUser: '', // 自检人
        submitTime: '', // 时间-当前日期
        measure: '', // 拟采取措施
        measureFileInfoList: [] // 文件
      },
      rules: {
        measure: [{ required: true, message: '请填写拟采取措施', trigger: 'change' }]
      },
      // 流程配置
      processConfig: null,
      processConfig1: null,
      processConfig2: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId: null
    }
  },

  components: {},

  computed: {},

  watch: {
    form: {
      handler(val) {
        val && val.checkUser && (this.formBox = this.form)
      },
      immediate: true,
      deep: true
    },
    proposedMeasuresStatus: {
      handler(val) {
        // 拟采取不通过的，点击填写 回显上一次的信息
        val &&
          this.filterProposedMeasuresName(this.row.measureStatus) === '审核不通过' &&
          this.$listeners.showPropseMeasuresPrevInfo &&
          (this.spinning = true) &&
          this.$listeners.showPropseMeasuresPrevInfo((v) => {
            this.formBox.measure = v.measure
            this.formBox.measureFileInfoList = v.measureFileInfoList ? v.measureFileInfoList : []
            this.spinning = false
          })
      },
      immediate: true
    }
  },

  async created() {
    if (!this.form) {
      this.formBox.checkUser = this.userInfo.name
      this.formBox.submitTime = this.moment().format('YYYY-MM-DD')
    }
    //获取通过获取流程配置值
    // 蓝色预警
    this.processConfig = await this.$getConfigFn('project_cycle_early_warn_bule_measure')
    // 黄色预警
    this.processConfig1 = await this.$getConfigFn('project_cycle_early_warn_yellow_measure')
    // 红色预警
    this.processConfig2 = await this.$getConfigFn('project_cycle_early_warn_red_measure')
  },

  mounted() {},

  methods: {
    addWarningFormHandler() {
      this.$refs.ProposedMeasuresCardForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.setProcessConfig = null
          let processConfig = null
          switch (this.row.warnLevel) {
            case 0:
              // 蓝色预警
              processConfig = this.processConfig
              break
            case 1:
              // 黄色预警
              processConfig = this.processConfig1
              break
            case 2:
              // 红色预警
              processConfig = this.processConfig2
              break
          }
          this.submitApprovalConfirm(processConfig, (flag)=>{
            if(!flag) {
              this.loading = false
              return
            }
            fillMeasure({ ...this.formBox, id: this.row.id })
                    .then((res) => {
                      this.$message.success('添加成功')
                      this.businessId = res.data.id
                      switch (this.row.warnLevel) {
                        case 0:
                          // 蓝色预警
                          this.setProcessConfig = this.processConfig
                          break
                        case 1:
                          // 黄色预警
                          this.setProcessConfig = this.processConfig1
                          break
                        case 2:
                          // 红色预警
                          this.setProcessConfig = this.processConfig2
                          break
                      }
                      //流程什么都没有配置，就自动关闭弹框
                      if(this.setProcessConfig*1 === 4){
                        this.closeSelectApprover();
                      }
                    })
                    .finally(() => {
                      this.loading = false
                    })
          })

        }else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
    /**
     * @description: 选择审批人关闭回调时关闭提交弹窗
     * @return {void}
     */
    closeSelectApprover() {
      this.$emit('search')
      this.handleClose()
    },
    handleClose() {
      this.$refs.ProposedMeasuresCardForm.resetFields()
      this.formBox.measureFileInfoList = []
      this.$emit('update:proposedMeasuresStatus', false)
    }
  }
}
</script>
<style lang='less' scoped>
</style>
